
from django import template
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter

import markdown

register = template.Library()


@register.filter(is_safe=True)
@stringfilter
def custom_markdown(value):
    content = mark_safe(markdown.markdown(value,
                                          output_format='html5',
                                          extensions=[
                                              'markdown.extensions.extra',
                                              'markdown.extensions.fenced_code',
                                              'markdown.extensions.tables',
                                              'markdown.extensions.codehilite',  # 语法高亮拓展
                                              'markdown.extensions.toc'  # 自动生成目录
                                          ],
                                          safe_mode=True,
                                          enable_attributes=False))
    return content


@register.simple_tag
def test_simple_tag(*args, **kwargs):
    return '{}-{}'.format('_'.join(args), '*'.join(kwargs.values()))


@register.inclusion_tag('extends/page.html')
def pagination(num):
    return {'num': range(1, num + 1)}


@register.filter
def set_remote(value):
    value = value.replace("b'", "").replace("'", "")
    return value
